<?php
/**
 * 手机支付方式
 *
 *
 *
*/

defined('IN_NWIDC') or exit('Access Invalid!');
class mb_paymentControl extends SystemControl{
    public function __construct(){
        parent::__construct();
    }

    public function indexOp() {
        $this->payment_listOp();
    }

    public function payment_listOp() {
        $model_mb_payment = Model('mb_payment');
        $mb_payment_list = $model_mb_payment->getMbPaymentList();
        Tpl::output('mb_payment_list', $mb_payment_list);
        Tpl::showpage('mb_payment.list');
    }

    public function ini_file($inifilename,$key,$value=null) {
        if(!file_exists($inifilename)) exit("文件不存在");

        $confarr = parse_ini_file($inifilename,true);

        $newini = '';

        foreach ($confarr as $k => $v)
        {
            if($k==$key){
                $v = $value;
            }
            $newini .= $k."=".$v."\r\n";
        }

        if ( ($fi = fopen($inifilename,"w")) )
        {
            flock($fi, LOCK_EX);//排它锁
            fwrite($fi, $newini);
            flock($fi, LOCK_UN);
            fclose($fi);
            return true;
        }
        exit("没有写入权限");//写文件失败
    }

    /**
     * 编辑
     */
    public function payment_editOp() {
        $payment_id = intval($_GET["payment_id"]);

        $model_mb_payment = Model('mb_payment');

        $mb_payment_info = $model_mb_payment->getMbPaymentInfo(array('payment_id' => $payment_id));

        if($mb_payment_info['payment_code'] == 'abcpay' || $mb_payment_info['payment_code'] == 'abcpay2'){
            $payment_config = parse_ini_file(BASE_ROOT_PATH."/mobile/api/payment/".$mb_payment_info['payment_code']."/ebusclient/TrustMerchant.ini");
            $mb_payment_info['payment_config']['merchantid'] = $payment_config['MerchantID'];
            $mb_payment_info['payment_config']['merchantcertpassword'] = $payment_config['MerchantCertPassword'];
            Tpl::output('payment',$mb_payment_info);
        }else{
            Tpl::output('payment', $mb_payment_info);
        }

        Tpl::output('payment', $mb_payment_info);
        Tpl::showpage('mb_payment.edit');
    }

    /**
     * 编辑保存
     */
    public function payment_saveOp() {
        $payment_id = intval($_POST["payment_id"]);
        $payment_code = $_POST["payment_code"];
        $data = array();
        $data['payment_state'] = intval($_POST["payment_state"]);

        switch ($_POST['payment_code']) {
            case 'alipay':
                $payment_config = array(
                    'alipay_account' => $_POST['alipay_account'],
                    'alipay_key' => $_POST['alipay_key'],
                    'alipay_partner' => $_POST['alipay_partner'],
                );
                break;
            case 'wxpay':
                $payment_config = array(
                    'wxpay_partner' => $_POST['wxpay_partner'],
                    'wxpay_key' => $_POST['wxpay_key'],
                );
                break;
            case 'abcpay':
                $payment_config = array(
                    'merchantid' => $_POST['merchantid'],
                    'merchantcertpassword' => $_POST['merchantcertpassword'],
                );
                break;
            case 'abcpay2':
                $payment_config = array(
                    'merchantid' => $_POST['merchantid'],
                    'merchantcertpassword' => $_POST['merchantcertpassword'],
                );
                break;
            case 'joinpay1':
                $payment_config = array(
                  'merchantNo' => $_POST['merchantNo'],
                  'merchantKey' => $_POST['merchantKey'],
                );
                break;
            case 'joinpay2':
                $payment_config = array(
                  'merchantNo' => $_POST['merchantNo'],
                  'merchantKey' => $_POST['merchantKey'],
                  'appId' => $_POST['appId'],
                );
                break;
            default:
                showMessage(L('param_error'), '');
        }
        $data['payment_config'] = $payment_config;

        $model_mb_payment = Model('mb_payment');

        if ($payment_code == 'abcpay' || $payment_code == 'abcpay2') {
            if(count($_FILES)){
                $path = dirname(BASE_PATH) . '/mobile/api/payment/'.$payment_code.'/cert/merchant.pfx';
                @move_uploaded_file($_FILES['file']['tmp_name'],$path);
            }

            $ini_file = BASE_ROOT_PATH."/mobile/api/payment/".$payment_code."/ebusclient/TrustMerchant.ini";
            $this->ini_file($ini_file,'MerchantID',$_POST['merchantid']);
            $this->ini_file($ini_file,'MerchantCertPassword',$_POST['merchantcertpassword']);
            $this->ini_file($ini_file,'TrustPayCertFile',BASE_ROOT_PATH."/mobile/api/payment/".$payment_code."/cert/TrustPay.cer");
            $this->ini_file($ini_file,'MerchantCertFile',BASE_ROOT_PATH."/mobile/api/payment/".$payment_code."/cert/merchant.pfx");
            $this->ini_file($ini_file,'LogPath',BASE_ROOT_PATH."/mobile/api/payment/".$payment_code);
            $result = true;
        }

        $result = $model_mb_payment->editMbPayment($data, array('payment_id' => $payment_id));


        if($result) {
            showMessage(Language::get('nc_common_save_succ'), urlAdmin('mb_payment', 'payment_list'));
        } else {
            showMessage(Language::get('nc_common_save_fail'), urlAdmin('mb_payment', 'payment_list'));
        }
    }
}
